<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>code_metrics &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="dead_code_scanner" href="dead_code_scanner.html" />
    <link rel="prev" title="assertions" href="assertions.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../userman/index.html">User Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Developer Tools</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="overview.html">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="asdf.html"><code class="docutils literal notranslate"><span class="pre">asdf</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="assertions.html"><code class="docutils literal notranslate"><span class="pre">assertions</span></code></a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">code_metrics</span></code></a><ul>
<li class="toctree-l3"><a class="reference internal" href="#api-documentation">API documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#loading">Loading</a></li>
<li class="toctree-l3"><a class="reference internal" href="#testing">Testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="#available-metrics">Available metrics</a></li>
<li class="toctree-l3"><a class="reference internal" href="#coupling-metrics">Coupling metrics</a></li>
<li class="toctree-l3"><a class="reference internal" href="#halstead-metric">Halstead metric</a></li>
<li class="toctree-l3"><a class="reference internal" href="#upn-metric">UPN metric</a></li>
<li class="toctree-l3"><a class="reference internal" href="#cyclomatic-complexity-metric">Cyclomatic complexity metric</a></li>
<li class="toctree-l3"><a class="reference internal" href="#usage">Usage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#excluding-code-from-analysis">Excluding code from analysis</a></li>
<li class="toctree-l3"><a class="reference internal" href="#defining-new-metrics">Defining new metrics</a></li>
<li class="toctree-l3"><a class="reference internal" href="#third-party-tools">Third-party tools</a></li>
<li class="toctree-l3"><a class="reference internal" href="#applying-metrics-to-prolog-modules">Applying metrics to Prolog modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="#applying-metrics-to-plain-prolog-code">Applying metrics to plain Prolog code</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="dead_code_scanner.html"><code class="docutils literal notranslate"><span class="pre">dead_code_scanner</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="debug_messages.html"><code class="docutils literal notranslate"><span class="pre">debug_messages</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="debugger.html"><code class="docutils literal notranslate"><span class="pre">debugger</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="diagrams.html"><code class="docutils literal notranslate"><span class="pre">diagrams</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="doclet.html"><code class="docutils literal notranslate"><span class="pre">doclet</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="help.html"><code class="docutils literal notranslate"><span class="pre">help</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="issue_creator.html"><code class="docutils literal notranslate"><span class="pre">issue_creator</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="lgtdoc.html"><code class="docutils literal notranslate"><span class="pre">lgtdoc</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="lgtunit.html"><code class="docutils literal notranslate"><span class="pre">lgtunit</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="linter.html"><code class="docutils literal notranslate"><span class="pre">linter</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="make.html"><code class="docutils literal notranslate"><span class="pre">make</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="packs.html"><code class="docutils literal notranslate"><span class="pre">packs</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="ports_profiler.html"><code class="docutils literal notranslate"><span class="pre">ports_profiler</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="profiler.html"><code class="docutils literal notranslate"><span class="pre">profiler</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="tutor.html"><code class="docutils literal notranslate"><span class="pre">tutor</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="wrapper.html"><code class="docutils literal notranslate"><span class="pre">wrapper</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">Developer Tools</a></li>
      <li class="breadcrumb-item active"><code class="docutils literal notranslate"><span class="pre">code_metrics</span></code></li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/devtools/code_metrics.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="code-metrics">
<span id="library-code-metrics"></span><h1><code class="docutils literal notranslate"><span class="pre">code_metrics</span></code><a class="headerlink" href="#code-metrics" title="Link to this heading"></a></h1>
<p>The purpose of this tool is to assess qualities of source code that may
predict negative aspects such as entity coupling, cohesion, complexity,
error-proneness, and overall maintainability. It is meant to be
extensible via the addition of objects implementing new metrics.</p>
<p>This tool provides predicates for computing metrics for source files,
entities, libraries, files, and directories. The actual availability of
a particular predicate depends on the specific metric. A set of
predicates prints, by default, the computed metric values to the
standard output. A second set of predicates computes and returns a score
(usually a compound term with the computed metric values as arguments).</p>
<section id="api-documentation">
<h2>API documentation<a class="headerlink" href="#api-documentation" title="Link to this heading"></a></h2>
<p>This tool API documentation is available at:</p>
<p><a class="reference external" href="../../apis/library_index.html#code-metrics">../../apis/library_index.html#code-metrics</a></p>
</section>
<section id="loading">
<h2>Loading<a class="headerlink" href="#loading" title="Link to this heading"></a></h2>
<p>This tool can be loaded using the query:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(code_metrics(loader)).
</pre></div>
</div>
</section>
<section id="testing">
<h2>Testing<a class="headerlink" href="#testing" title="Link to this heading"></a></h2>
<p>To test this tool, load the <code class="docutils literal notranslate"><span class="pre">tester.lgt</span></code> file:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(code_metrics(tester)).
</pre></div>
</div>
</section>
<section id="available-metrics">
<h2>Available metrics<a class="headerlink" href="#available-metrics" title="Link to this heading"></a></h2>
<p>Currently, the following metrics are provided:</p>
<ul class="simple">
<li><p>Number of Clauses (<code class="docutils literal notranslate"><span class="pre">noc_metric</span></code>)</p></li>
<li><p>Number of Rules (<code class="docutils literal notranslate"><span class="pre">nor_metric</span></code>)</p></li>
<li><p>Unique Predicate Nodes (<code class="docutils literal notranslate"><span class="pre">upn_metric</span></code>)</p></li>
<li><p>Cyclomatic Complexity (<code class="docutils literal notranslate"><span class="pre">cc_metric</span></code>)</p></li>
<li><p>Depth of Inheritance (<code class="docutils literal notranslate"><span class="pre">dit_metric</span></code>)</p></li>
<li><p>Efferent coupling, afferent coupling, instability, and abstractness
(<code class="docutils literal notranslate"><span class="pre">coupling_metric</span></code>)</p></li>
<li><p>Documentation (<code class="docutils literal notranslate"><span class="pre">doc_metric</span></code>)</p></li>
<li><p>Source code size (<code class="docutils literal notranslate"><span class="pre">size_metric</span></code>)</p></li>
<li><p>Halstead complexity (<code class="docutils literal notranslate"><span class="pre">halstead_metric</span></code> and
<code class="docutils literal notranslate"><span class="pre">halstead_metric(Stroud)</span></code>)</p></li>
</ul>
<p>A helper object, <code class="docutils literal notranslate"><span class="pre">code_metrics</span></code>, is also provided, allowing running
all loaded individual metrics. For code coverage metrics, see the
<code class="docutils literal notranslate"><span class="pre">lgtunit</span></code> tool documentation.</p>
</section>
<section id="coupling-metrics">
<h2>Coupling metrics<a class="headerlink" href="#coupling-metrics" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>Efferent coupling (<code class="docutils literal notranslate"><span class="pre">Ce</span></code>): Number of entities that an entity depends
on. These include objects receiving messages from the entity, plus the
implemented protocols, imported categories, and
extended/instantiated/specialized objects.</p></li>
<li><p>Afferent coupling (<code class="docutils literal notranslate"><span class="pre">Ca</span></code>): Number of entities that depend on an
entity. For a protocol, the number of protocols that extend it, plus
the number of objects and categories that implement it. For a
category, the number of objects that import it. For an object, the
number of categories and objects that send messages to it plus the
number of objects that extend/instantiate/specialize it.</p></li>
<li><p>Instability: Computed as <code class="docutils literal notranslate"><span class="pre">Ce</span> <span class="pre">/</span> <span class="pre">(Ce</span> <span class="pre">+</span> <span class="pre">Ca)</span></code>. Measures an entity
resilience to change. Ranging from 0.0 to 1.0, with 0.0 indicating a
maximally stable entity and 1.0 indicating a maximally unstable
entity. Ideally, an entity is either maximally stable or maximally
unstable.</p></li>
<li><p>Abstractness: Computed as the ratio between the number of static
predicates with scope directives without a local definition and the
number of static predicates with scope directives. Measures the
rigidity of an entity. Ranging from 0.0 to 1.0, with 0.0 indicating a
fully concrete entity and 1.0 indicating a fully abstract entity.</p></li>
</ul>
<p>The dependencies count includes direct entity relations plus predicate
calls or dynamic updates to predicates in external objects or
categories.</p>
<p>For more information on the interpretation of the coupling metric
scores, see, e.g., the original paper by Robert Martin:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="o">@</span>inproceedings<span class="k">{</span>citeulike<span class="o">:</span><span class="m">1579528</span>,
    author <span class="o">=</span> <span class="s">&quot;Martin, Robert&quot;</span>,
    booktitle <span class="o">=</span> <span class="s">&quot;Workshop Pragmatic and Theoretical Directions in Object-Oriented Software Metrics&quot;</span>,
    citeulike<span class="o">-</span>article<span class="o">-</span>id <span class="o">=</span> <span class="m">1579528</span>,
    citeulike<span class="o">-</span>linkout<span class="o">-</span><span class="m">0</span> <span class="o">=</span> <span class="s">&quot;http://www.objectmentor.com/resources/articles/oodmetrc.pdf&quot;</span>,
    keywords <span class="o">=</span> <span class="s">&quot;diplomarbeit&quot;</span>,
    organization <span class="o">=</span> <span class="s">&quot;OOPSLA&#39;94&quot;</span>,
    posted<span class="o">-</span>at <span class="o">=</span> <span class="s">&quot;2007-08-21 11:08:44&quot;</span>,
    priority <span class="o">=</span> <span class="m">0</span>,
    title <span class="o">=</span> <span class="s">&quot;OO Design Quality Metrics - An Analysis of Dependencies&quot;</span>,
    url <span class="o">=</span> <span class="s">&quot;http://www.objectmentor.com/resources/articles/oodmetrc.pdf&quot;</span>,
    year <span class="o">=</span> <span class="m">1994</span>
<span class="k">}</span>
</pre></div>
</div>
<p>The coupling metric was also influenced by the metrics rating system in
Microsoft Visual Studio and aims to eventually emulate the functionality
of a maintainability index score.</p>
</section>
<section id="halstead-metric">
<h2>Halstead metric<a class="headerlink" href="#halstead-metric" title="Link to this heading"></a></h2>
<p>Predicates declared, user-defined, and called are interpreted as
<em>operators</em>. Built-in predicates and built-in control constructs are
ignored. Predicate arguments are abstracted, assumed distinct, and
interpreted as <em>operands</em>. Note that this definition of operands is a
significant deviation from the original definition, which used syntactic
literals. A computation closer to the original definition of the metric
would require switching to use the parser to collect information on
syntactic literals, which would imply a much larger computation cost.
The number of predicate calls doesn’t include calls to built-in
predicates and can underestimate recursive calls.</p>
<p>The computation of this metric is parameterized by the <em>Stroud</em>
coefficient for computing the time required to program (default is 18).
The following individual measures are computed:</p>
<ul class="simple">
<li><p>Number of distinct predicates (declared, defined, called, or updated;
<code class="docutils literal notranslate"><span class="pre">Pn</span></code>).</p></li>
<li><p>Number of predicate arguments (assumed distinct; <code class="docutils literal notranslate"><span class="pre">PAn</span></code>).</p></li>
<li><p>Number of predicate calls/updates + number of clauses (<code class="docutils literal notranslate"><span class="pre">Cn</span></code>).</p></li>
<li><p>Number of predicate call/update arguments + number of clause head
arguments (<code class="docutils literal notranslate"><span class="pre">CAn</span></code>).</p></li>
<li><p>Entity vocabulary (<code class="docutils literal notranslate"><span class="pre">EV</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">EV</span> <span class="pre">=</span> <span class="pre">Pn</span> <span class="pre">+</span> <span class="pre">PAn</span></code>.</p></li>
<li><p>Entity length (<code class="docutils literal notranslate"><span class="pre">EL</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">EL</span> <span class="pre">=</span> <span class="pre">Cn</span> <span class="pre">+</span> <span class="pre">CAn</span></code>.</p></li>
<li><p>Volume (<code class="docutils literal notranslate"><span class="pre">V</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">V</span> <span class="pre">=</span> <span class="pre">EL</span> <span class="pre">*</span> <span class="pre">log2(EV)</span></code>.</p></li>
<li><p>Difficulty (<code class="docutils literal notranslate"><span class="pre">D</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">D</span> <span class="pre">=</span> <span class="pre">(Pn/2)</span> <span class="pre">*</span> <span class="pre">(CAn/An)</span></code>.</p></li>
<li><p>Effort (<code class="docutils literal notranslate"><span class="pre">E</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">E</span> <span class="pre">=</span> <span class="pre">D</span> <span class="pre">*</span> <span class="pre">V</span></code>.</p></li>
<li><p>Time required to program (<code class="docutils literal notranslate"><span class="pre">T</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">T</span> <span class="pre">=</span> <span class="pre">E/k</span></code> seconds
(where <code class="docutils literal notranslate"><span class="pre">k</span></code> is the Stroud number; defaults to 18).</p></li>
<li><p>Number of delivered bugs (<code class="docutils literal notranslate"><span class="pre">B</span></code>). Computed as <code class="docutils literal notranslate"><span class="pre">B</span> <span class="pre">=</span> <span class="pre">V/3000</span></code>.</p></li>
</ul>
</section>
<section id="upn-metric">
<h2>UPN metric<a class="headerlink" href="#upn-metric" title="Link to this heading"></a></h2>
<p>The Unique Predicate Nodes (UPN) metric is described in the following
paper:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="o">@</span>article<span class="k">{</span><span class="nv">MOORES199845</span>,
    title <span class="o">=</span> <span class="s">&quot;Applying Complexity Measures to Rule-Based Prolog Programs&quot;</span>,
    journal <span class="o">=</span> <span class="s">&quot;Journal of Systems and Software&quot;</span>,
    volume <span class="o">=</span> <span class="s">&quot;44&quot;</span>,
    number <span class="o">=</span> <span class="s">&quot;1&quot;</span>,
    pages <span class="o">=</span> <span class="s">&quot;45 - 52&quot;</span>,
    year <span class="o">=</span> <span class="s">&quot;1998&quot;</span>,
    issn <span class="o">=</span> <span class="s">&quot;0164-1212&quot;</span>,
    doi <span class="o">=</span> <span class="s">&quot;https://doi.org/10.1016/S0164-1212(98)10042-0&quot;</span>,
    url <span class="o">=</span> <span class="s">&quot;http://www.sciencedirect.com/science/article/pii/S0164121298100420&quot;</span>,
    author <span class="o">=</span> <span class="s">&quot;Trevor T Moores&quot;</span>
<span class="k">}</span>
</pre></div>
</div>
<p>The nodes include called and updated predicates independently of where
they are defined. It also includes multifile predicates contributed to
other entities.</p>
</section>
<section id="cyclomatic-complexity-metric">
<h2>Cyclomatic complexity metric<a class="headerlink" href="#cyclomatic-complexity-metric" title="Link to this heading"></a></h2>
<p>The cyclomatic complexity metric evaluates an entity code complexity by
measuring the number of linearly independent paths through the code. In
its current implementation, all defined predicates that are not called
or updated are counted as graph-connected components (the reasoning
being that these predicates can be considered entry points). The
implementation uses the same predicate abstraction as the UPN metric.
The defined predicates include multifile predicate definitions
contributed by the entity to other entities.</p>
<p>For more details on this metric, see the original paper by Thomas J.
McCabe:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="o">@</span>inproceedings<span class="k">{</span><span class="nv">McCabe</span><span class="o">:</span><span class="m">1976</span><span class="o">:</span><span class="nv">CM</span><span class="o">:</span><span class="m">800253.807712</span>,
    author <span class="o">=</span> <span class="s">&quot;McCabe, Thomas J.&quot;</span>,
    title <span class="o">=</span> <span class="s">&quot;A Complexity Measure&quot;</span>,
    booktitle <span class="o">=</span> <span class="s">&quot;Proceedings of the 2Nd International Conference on Software Engineering&quot;</span>,
    series <span class="o">=</span> <span class="s">&quot;ICSE &#39;76&quot;</span>,
    year <span class="o">=</span> <span class="m">1976</span>,
    location <span class="o">=</span> <span class="s">&quot;San Francisco, California, USA&quot;</span>,
    pages <span class="o">=</span> <span class="s">&quot;407--&quot;</span>,
    url <span class="o">=</span> <span class="s">&quot;http://dl.acm.org/citation.cfm?id=800253.807712&quot;</span>,
    acmid <span class="o">=</span> <span class="m">807712</span>,
    publisher <span class="o">=</span> <span class="s">&quot;IEEE Computer Society Press&quot;</span>,
    address <span class="o">=</span> <span class="s">&quot;Los Alamitos, CA, USA&quot;</span>,
    keywords <span class="o">=</span> <span class="s">&quot;Basis, Complexity measure, Control flow, Decomposition, Graph theory, Independence, Linear, Modularization, Programming, Reduction, Software, Testing&quot;</span>,
<span class="k">}</span>
</pre></div>
</div>
</section>
<section id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Link to this heading"></a></h2>
<p>All metrics require the source code to be analyzed to be loaded with the
<code class="docutils literal notranslate"><span class="pre">source_data</span></code> flag turned on. For usage examples, see the
<code class="docutils literal notranslate"><span class="pre">SCRIPT.txt</span></code> file in the tool directory.</p>
<p>Be sure to fully understand the metrics individual meanings and any
implementation limitations before using them to support any evaluation
or decision process.</p>
</section>
<section id="excluding-code-from-analysis">
<h2>Excluding code from analysis<a class="headerlink" href="#excluding-code-from-analysis" title="Link to this heading"></a></h2>
<p>A set of options is available to specify code that should be excluded
when applying code metrics:</p>
<ul>
<li><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">exclude_directories(Directories)</span></code></div>
<div class="line">list of directories to exclude (default is <code class="docutils literal notranslate"><span class="pre">[]</span></code>); all
sub-directories of the excluded directories are also excluded;
directories may be listed by full or relative path</div>
</div>
</li>
<li><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">exclude_files(Files)</span></code></div>
<div class="line">list of source files to exclude (default is <code class="docutils literal notranslate"><span class="pre">[]</span></code>); files may be
listed by full path or basename, with or without extension</div>
</div>
</li>
<li><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">exclude_libraries(Libraries)</span></code></div>
<div class="line">list of libraries to exclude (default is
<code class="docutils literal notranslate"><span class="pre">[startup,</span> <span class="pre">scratch_directory]</span></code>)</div>
</div>
</li>
<li><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">exclude_entities(Entities)</span></code></div>
<div class="line">list of entities to exclude (default is <code class="docutils literal notranslate"><span class="pre">[]</span></code>)</div>
</div>
</li>
</ul>
</section>
<section id="defining-new-metrics">
<h2>Defining new metrics<a class="headerlink" href="#defining-new-metrics" title="Link to this heading"></a></h2>
<p>New metrics can be implemented by defining an object that imports the
<code class="docutils literal notranslate"><span class="pre">code_metric</span></code> category and implements its score predicates. There is
also a <code class="docutils literal notranslate"><span class="pre">code_metrics_utilities</span></code> category that defines useful
predicates for the definition of metrics.</p>
</section>
<section id="third-party-tools">
<h2>Third-party tools<a class="headerlink" href="#third-party-tools" title="Link to this heading"></a></h2>
<p>The following open-source command-line programs can count blank lines,
comment lines, and lines of source code in many programming languages,
including Logtalk:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">cloc</span></code> - <a class="reference external" href="https://github.com/AlDanial/cloc">https://github.com/AlDanial/cloc</a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">ohcount</span></code> - <a class="reference external" href="https://github.com/blackducksoftware/ohcount">https://github.com/blackducksoftware/ohcount</a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">tokei</span></code> - <a class="reference external" href="https://github.com/XAMPPRocky/tokei">https://github.com/XAMPPRocky/tokei</a></p></li>
</ul>
</section>
<section id="applying-metrics-to-prolog-modules">
<h2>Applying metrics to Prolog modules<a class="headerlink" href="#applying-metrics-to-prolog-modules" title="Link to this heading"></a></h2>
<p>Some of the metrics can also be applied to Prolog modules that Logtalk
is able to compile as objects. For example, if the Prolog module file is
named <code class="docutils literal notranslate"><span class="pre">module.pl</span></code>, try:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(module, [source_data(on)]).
</pre></div>
</div>
<p>Due to the lack of standardization of module systems and the abundance
of proprietary extensions, this solution is not expected to work for all
cases.</p>
</section>
<section id="applying-metrics-to-plain-prolog-code">
<h2>Applying metrics to plain Prolog code<a class="headerlink" href="#applying-metrics-to-plain-prolog-code" title="Link to this heading"></a></h2>
<p>Some of the metrics can also be applied to plain Prolog code. For
example, if the Prolog file is named <code class="docutils literal notranslate"><span class="pre">code.pl</span></code>, simply define an
object including its code:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(code).
<span class="p">    :- </span><span class="k">include</span>(<span class="s">&#39;code.pl&#39;</span>).
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>Save the object to an e.g. <code class="docutils literal notranslate"><span class="pre">code.lgt</span></code> file in the same directory as
the Prolog file and then load it in debug mode:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(code, [source_data(on)]).
</pre></div>
</div>
<p>In alternative, use the <code class="docutils literal notranslate"><span class="pre">object_wrapper_hook</span></code> provided by the
<code class="docutils literal notranslate"><span class="pre">hook_objects</span></code> library:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(hook_objects(loader)).
...

| <span class="o">?-</span> <span class="k">logtalk_load</span>(code, [hook(object_wrapper_hook), source_data(on)]).
</pre></div>
</div>
<p>With either wrapping solution, pay special attention to any compilation
warnings that may signal issues that could prevent the plain Prolog code
from working when wrapped by an object.</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="assertions.html" class="btn btn-neutral float-left" title="assertions" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="dead_code_scanner.html" class="btn btn-neutral float-right" title="dead_code_scanner" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>